package com.wslNB.controller;

import com.wslNB.domain.User;
import com.wslNB.mapper.UserMapper;
import com.wslNB.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.redis.core.RedisTemplate;

@RestController
@RequestMapping("/login")
public class LoginController {


   @Autowired
   private RedisTemplate redisTemplate;
   @Autowired
   private UserMapper userMapper;

    //生成短信验证码
    @GetMapping("/checkNumber")
    public String checkNumber(){

        try {
            Integer checkCode = ValidateCodeUtils.generateValidateCode(4);
            //模拟发送短信
            System.out.println("checkCode验证码："+checkCode);
            //保存到redis中
            redisTemplate.boundValueOps("checkNumber").set(checkCode);
        } catch (Exception exception) {
            exception.printStackTrace();
            return "验证码发送失败";
        }
        return "验证码发送成功！";
    }

    //判断登录 查询寻用户
    @GetMapping("/userLogin")
    public User findByPhoneAndPassword(User user){

        try {
            return userMapper.findByPhoneAndPassword(user);
        } catch (Exception exception) {
            exception.printStackTrace();
            return null;
        }

    }

}
